layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index', //主入口模块
    tableSearch: '../layui/lay/modules/tableSearch/tableSearch',
}).use(['index', 'table', 'laytpl', 'tableSearch'], function(){
    var $ = layui.$,
        table = layui.table,
        tableSearch = layui.tableSearch;

    table.render({
        id: 'data-table',
        elem: '#data-table',
        url: 'dataStatis/inviteRecharge/list',
        where: getData(),
        toolbar: '#data-table-toolbar',
        title: '邀请充值列表',
        height: 'full-30',
        page: true,
        limit: 50,
        cols: [[
            {field: 'inviteUserId', title: '邀请者ID', width: 160, align: 'center'},
            {field: 'name', title: '昵称', align: 'center'},
            {field: 'host', title: '身份', align: 'center', templet: '#hostTpl'},
            {field: 'registerNum', title: '注册量', align: 'center'},
            {field: 'nowRegisterNum', title: '当日注册人数', width: 115, align: 'center'},
            {field: 'nowRechargeNum', title: '当日充值人数', width: 115, align: 'center'},
            {field: 'hostRate', title: '主播比例', align: 'center', templet: '#hostRateTpl'},
            {field: 'rechargeNum', title: '充值人数', align: 'center'},
            {field: 'rechargeRate', title: '转化率', align: 'center', templet: '#rechargeRateTpl'},
            {field: 'totalOrderNum', title: '订单数', align: 'center'},
            {field: 'rechargeAmount', title: '充值总金额', align: 'center'},
            {field: 'online5Times', title: '在线时间统计', width: 125, align: 'center', templet: '#onlineTimesTpl'}
        ]],
        done: function(res, curr, count) {
            var time = tableSearch.cache.data["time"];
            if(time != null && time != ''){
                $("[data-field='nowRegisterNum']").css('display','none');
                $("[data-field='nowRechargeNum']").css('display','none');
            }

            //表格reload之后重新加载查询条件
            if($('#data-table').next().find('.layui-table-header').find('tr').length < 2){
                initSearch.reload();
            }
        },
        parseData: function(result){
            return {
                "code": result.code, //解析接口状态
                "msg": result.msg, //解析提示文本
                "count": result.data.total, //解析数据长度
                "data": result.data.inviteRechargeList //解析数据列表
            };
        }
    });

    //加载查询条件
    var initSearch = tableSearch.render({
        'elem': '#data-table',
        'searchs': [
            {field: 'inviteUserId', type: 'input'}
        ],
        'searchBarData': [
            {field: 'time', type: 'date', format: 'datetime'}
        ],
        'done': function(res){
            // reloadData();
        }
    });

    //头工具栏事件
    table.on('toolbar(data-table)', function(obj){
        var checks = table.checkStatus(obj.config.id);
        var data = checks.data;
        switch(obj.event){
            case 'search':
                var data =  getData();
                table.reload('data-table', {
                    url: 'dataStatis/inviteRecharge/list',
                    where: data,
                    page: {
                        curr: 1
                    }
                });
                break;
            case 'reset':
                tableSearch.resetData({});
                break;
        };
    });

    /**
     * 获取查询数据
     * @returns {{host: jQuery, online: jQuery, keyword: (*|jQuery|Array|string|undefined), status: (*|jQuery|Array|string|undefined)}}
     */
    function getData() {
        return tableSearch.getData({});
    }
});